package com.example.qxapp.activity;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.qxapp.Bean.Post;
import com.example.qxapp.Bean.User;
import com.example.qxapp.R;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class Pushcontent extends AppCompatActivity {

    private EditText pushcontent;
    private ImageView back;
    private Button push;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pushcontent);
        init();

        //发帖
        push.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pushcontent.getText().toString().isEmpty()){
                    Toast.makeText(Pushcontent.this,"帖子内容为空，请输入内容",Toast.LENGTH_LONG).show();
                }else {
                    User user= BmobUser.getCurrentUser(User.class);//获取到现在登录的人
                    Post post=new Post();
                    post.setName(user.getUsername());
                    post.setContent(pushcontent.getText().toString());
                    post.setAuthor(user);
                    post.setIsrelated("0");
                    post.setUserOnlyId(user.getObjectId());
                    post.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e==null){
                                pushcontent.setText("");
                                Toast.makeText(Pushcontent.this,"帖子发布成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(Pushcontent.this,"帖子发布失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void init() {
        pushcontent=findViewById(R.id.pushcontent);
        back=findViewById(R.id.back);
        push=findViewById(R.id.push);
    }
}
